# 一、加载django配置环境
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_demo.settings.offline_config")


# 二、加载celery配置环境
from celery import Celery
# broker
#线下
broker = 'redis://:chenhonglong123@127.0.0.1:6379/0'
#线上
# broker = 'redis://:chenhonglong123@127.0.0.1:6380/0'
# backend
#线下
backend = 'redis://:chenhonglong123@127.0.0.1:6379/1'
#线上
# backend = 'redis://:chenhonglong123@127.0.0.1:6380/0'
# worker
app = Celery(broker=broker, backend=backend, include=['celery_worker.tasks'])


# 时区
app.conf.timezone = 'Asia/Shanghai'
# 是否使用UTC
app.conf.enable_utc = False

# 定时任务配置
from datetime import timedelta
app.conf.beat_schedule = {
    # 案例：django异步更新缓存
    'update-banner-cache': {
        'task': 'celery_worker.tasks.update_banner_cache',
        'schedule': timedelta(hours=1),
        'args': (),
    },
}

"""
celery框架工作流程
1）创建Celery框架对象app，配置broker和backend，得到的app就是worker
2）给worker对应的app添加可处理的任务函数，用include配置给worker的app
3）完成提供的任务的定时配置app.conf.beat_schedule
4）启动celery服务，运行worker，执行任务
5）启动beat服务，运行beat，添加任务
"""